﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports _1065081_1065104.KHDTO
Imports System.Data.OleDb
Namespace KHDAO
    Public Class CkhachhangDAO
        Inherits CKHdataprovider
#Region "contructor"
        Public Sub New()

        End Sub
#End Region
#Region "methods"
        Public Function AddNewnhanvien(ByVal nhanvien As Cinforkhachhang) As Boolean
            Me.connect()
            Dim bCheck As Boolean = True
            Dim query As String = "insert into KHACHHANG(MAKH,HOTENKH,CMND,NGAYSINH,DIACHI,DIENTHOAI,EMAIL,PWD) values(?,?,?,?,?,?,?,?)"

            Me.command = New SqlCommand(query, connection)
            Me.command.Parameters.Add(New SqlParameter("@MAKH", nhanvien.nhanvienid.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@HOTENKH", nhanvien.nhanvienname.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@CMND", nhanvien.indentitycard.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@NGAYSINH", nhanvien.birthday))
            ' Me.command.Parameters.Add(New SqlParameter("@DIACHI", nhanvien.address.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@DIENTHOAI", nhanvien.phonenumber.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@EMAIL", nhanvien.email.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@PWD", nhanvien.password.Trim()))

            Try
                Me.command.ExecuteNonQuery()
                Me.disconnect()
            Catch ex As Exception
                Me.disconnect()
                bCheck = False
                Throw ex
            End Try
            Return bCheck
        End Function
        Public Function Deletenhanvien(ByVal nhanvienID As String) As Boolean
            Me.connect()
            Dim bCheck As Boolean = True
            Dim query As String = "delete from KHACHHANG where MAKH = ?"

            Me.command = New SqlCommand(query, connection)
            Me.command.Parameters.Add(New SqlParameter("@MAKH", nhanvienID.Trim()))

            Try
                Me.command.ExecuteNonQuery()
                Me.disconnect()
            Catch ex As Exception
                Me.disconnect()
                bCheck = False
                Dim str As String = ex.Message
            End Try
            Return bCheck
        End Function
        Public Function Updatenhanvien(ByVal nhanvien As Cinforkhachhang) As Boolean
            Me.connect()
            Dim bCheck As Boolean = True
            Dim query As String = "update KHACHHANG set HOTENKH=?, CMND=?, NGAYSINH=?, DIACHI=?, DIENTHOAI=?, EMAIL=?, PWD=?  where MAKH = ?"

            Me.command = New SqlCommand(query, connection)
            Me.command.Parameters.Add(New SqlParameter("@HOTENKH", nhanvien.nhanvienName.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@CMND", nhanvien.indentitycard.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@NGAYSINH", nhanvien.Birthday))
            'Me.command.Parameters.Add(New SqlParameter("@DIACHI", nhanvien..Trim()))
            Me.command.Parameters.Add(New SqlParameter("@DIENTHOAI", nhanvien.PhoneNumber.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@EMAIL", nhanvien.EMail.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@PWD", nhanvien.Password.Trim()))
            Me.command.Parameters.Add(New SqlParameter("@MAKH", nhanvien.nhanvienID.Trim()))
            '    Me.command.Parameters.Add(New OleDbParameter("@HOTENKH", nhanvien.nhanvienName.Trim()))
            '   Me.command.Parameters.Add(New OleDbParameter("@CMND", nhanvien.IdentityCard.Trim()))
            '  Me.command.Parameters.Add(New OleDbParameter("@NGAYSINH", nhanvien.Birthday))
            ' Me.command.Parameters.Add(New OleDbParameter("@DIACHI", nhanvien.Address.Trim()))
            'Me.command.Parameters.Add(New OleDbParameter("@DIENTHOAI", nhanvien.PhoneNumber.Trim()))
            'Me.command.Parameters.Add(New OleDbParameter("@EMAIL", nhanvien.EMail.Trim()))
            'Me.command.Parameters.Add(New OleDbParameter("@PWD", nhanvien.Password.Trim()))
            'Me.command.Parameters.Add(New OleDbParameter("@MAKH", nhanvien.nhanvienID.Trim()))

            Try
                Me.command.ExecuteNonQuery()
                Me.disconnect()
            Catch ex As Exception
                Me.disconnect()
                bCheck = False
                Throw ex
            End Try
            Return bCheck
        End Function
        Public Function Searchnhanvien(ByVal idCode As String, ByVal cusName As String, ByVal cusIdentity As String, ByVal cusTel As String) As DataTable
            Try
                Dim query As String = String.Empty

                If idCode <> "" OrElse cusIdentity <> "" OrElse cusTel <> "" Then
                    query = ((("select MAKH,HOTENKH,CMND,NGAYSINH,DIACHI,DIENTHOAI,EMAIL from KHACHHANG where " & "MAKH='") + idCode & "'" & " or CMND='") + cusIdentity & "'" & " or DIENTHOAI='") + cusTel & "'"
                ElseIf cusName <> "" Then
                    query = ("select MAKH,HOTENKH,CMND,NGAYSINH,DIACHI,DIENTHOAI,EMAIL from KHACHHANG where " & "RTRIM(HOTENKH) like '%' + '") + cusName & "' + '%'"
                Else
                    query = "select MAKH,HOTENKH,CMND,NGAYSINH,DIACHI,DIENTHOAI,EMAIL from KHACHHANG"
                End If

                Me.connect()
                Dim sqlDataTable As DataTable = Me.Excutenonquery_Datatable(query)
                Me.disconnect()

                Return sqlDataTable
            Catch e As Exception
                Throw e
            End Try
        End Function

        Public Function GetnhanvienID(ByVal strID As String, ByVal field As String, ByVal table As String, ByVal length As Integer) As String
            Return Me.createIdCode(strID, field, table, length)
        End Function
#End Region
    End Class
End Namespace


